﻿using System.ComponentModel;

namespace Gmantis.Controls.DataGrid
{
    [TypeConverter(typeof(DataGridSortStateTypeConverter))]
    public class DataGridSortState : INotifyPropertyChanged
    {
        // Events
        public event PropertyChangedEventHandler PropertyChanged;

        // Methods
        internal DataGridSortState(DataGridSortDirection direction, int? index)
        {
            this.Direction = direction;
            if (index.HasValue && (index >= 0))
            {
                this.Index = index.Value;
            }
            else
            {
                this.Index = -1;
            }
        }

        private void OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        internal void Set(DataGridSortDirection direction, int? index)
        {
            this.Direction = direction;
            int? nullable = index;
            this.Index = nullable.HasValue ? nullable.GetValueOrDefault() : -1;
            this.OnPropertyChanged("Direction");
        }

        public override string ToString()
        {
            return (this.Direction.ToString() + ((this.Index >= 0) ? (", " + this.Index.ToString()) : ""));
        }

        // Properties
        public DataGridSortDirection Direction { get; private set; }

        public int Index { get; private set; }
    }
}
